package com.tech.zhidengxian.service.impl;

import com.tech.zhidengxian.mapper.CommentMapper;
import com.tech.zhidengxian.model.dataobject.CommentDO;
import com.tech.zhidengxian.model.dto.CommentDTO;
import com.tech.zhidengxian.model.dto.GoodsDTO;
import com.tech.zhidengxian.service.api.CommentService;
import com.tech.zhidengxian.service.api.GoodsService;
import com.tech.zhidengxian.util.JacksonUtils;
import com.tech.zhidengxian.util.ResultAssert;
import com.tech.zhidengxian.util.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private GoodsService goodsService;
    @Override
    public CommentDTO publishComment(CommentDTO commentDTO) {
        CommentDO commentDO = JacksonUtils.convert(commentDTO, CommentDO.class);
        commentDO.setCreateTime(TimeUtil.currentDate());
        commentDO.setIsDeleted(false);
        commentDO.setType("goods");
        int insertComment = commentMapper.insertComment(commentDO);
        if (insertComment>0){
            GoodsDTO goodsDTO = new GoodsDTO();
            goodsDTO.setId(commentDTO.getRelationId());
            goodsDTO.setCommentCount(1L);
            insertComment = goodsService.updateGoodsCount(goodsDTO);
        }
        ResultAssert.assertGreaterThanZero(insertComment,"评论失败");
        return JacksonUtils.convert(commentDO,CommentDTO.class);
    }

    @Override
    public List<CommentDTO> queryCommentListByGoodsId(Long goodsId) {
        List<CommentDO> commentDOS = commentMapper.queryCommentListByGoodsId(goodsId);
        return JacksonUtils.convertList(commentDOS, CommentDTO.class);
    }
}
